package com.zelin.user.service.impl;

import com.alibaba.fastjson.JSON;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zelin.user.entity.TbUser;
import com.zelin.user.dao.TbUserMapper;
import com.zelin.user.service.TbUserService;
/**
 * Created by WF on 2021/11/8 15:37
 */
@Service
public class TbUserServiceImpl extends ServiceImpl<TbUserMapper, TbUser> implements TbUserService{

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    //1. 发送验证码
    @Override
    public void sendCode(String phone) {
        //1.1 生成一个随机的验证码
        int min = 100000;   //六位数的最小值
        int max = 999999;   //六位数的最大值
        Random random = new Random();
        int code = random.nextInt(max-min);  //得到一个[0,max-min)范围的随机数
        if(code < min){
            code = code + min ;
        }
        System.out.println("生成的六位数的验证码是：" + code);

        //1.2 将生成的验证码放到redis中
        redisTemplate.opsForValue().set(phone,code + "",10, TimeUnit.DAYS);

        //1.3 发送手机码
        Map<String,String> map = new HashMap<>();
        map.put("phone",phone);
        map.put("signName","泽易购");
        map.put("templateCode","SMS_195720463");
        //{"code":"xxxx"}
        Map<String,String> small = new HashMap<>();
        small.put("code",code + "");
        map.put("templateParam", JSON.toJSONString(small));
        rabbitTemplate.convertAndSend("sms",map);
    }
}
